package vn.com.vega.chacha.v2;

import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.Asynctask.LoadLocalSong;
import vn.com.vega.chacha.v2.Asynctask.LoadLocalSong.LoadLocalSongListener;
import vn.com.vega.chacha.v2.player.PlayerService;
import vn.com.vega.music.utils.Util;
import vn.com.vega.music.view.adapter.SongListAdapter;
import vn.com.vega.music.view.custom.BottomPlayerView;
import vn.com.vega.music.view.custom.BottomPlayerView.ShowHideBottomPlayerListener;
import vn.com.vega.music.view.custom.LoadingView;
import vn.com.vega.music.view.custom.LoadingView.RetryListener;
import vn.com.vega.music.view.custom.TopTaskbarView;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class LocalSongScreen extends ActivityBase implements
		View.OnClickListener {
	private ListView mListSong;
	private SongListAdapter mAdapter;
	private LoadingView mLoadingView;
	private LoadLocalSong mLoadLocalSong;
	private TopTaskbarView mTopTaskbarView;
	private BottomPlayerView mBottomPlayer;
	private View mPlayerViewReplace;
	private View mPlayAllSong;
	private View mSelectSongPlay;

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		unregisterReceiver(receiver);
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.local_song_screen);
		mListSong = (ListView) findViewById(R.id.lv_list_song);
		mLoadingView = (LoadingView) findViewById(R.id.loadingView_loading_list_song_local);
		mTopTaskbarView = (TopTaskbarView) findViewById(R.id.topTaskbarView_song_local);
		mTopTaskbarView.showBackButton();
		mTopTaskbarView.changeToTitleOnly("Nhạc trong máy");
		mLoadingView.setRetryListener(retryListener);

		mSelectSongPlay = findViewById(R.id.tv_select_song_play);
		mPlayAllSong = findViewById(R.id.tv_play_all_song);
		mSelectSongPlay.setOnClickListener(this);
		mPlayAllSong.setOnClickListener(this);

		mPlayerViewReplace = findViewById(R.id.view_replace_bottom_player);
		mBottomPlayer = (BottomPlayerView) findViewById(R.id.bottomPlayerView_song_local);
		mBottomPlayer.setShowHideListener(showHideBottomPlayerListener);

		loadLocalSong();
		IntentFilter intentFilter = new IntentFilter(
				PlayerService.PLAYSTATE_CHANGED);
		registerReceiver(receiver, intentFilter);
	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (PlayerService.PLAYSTATE_CHANGED.equalsIgnoreCase(action)) {
				if (mAdapter != null && mAdapter.getCount() > 0) {
					mAdapter.setCurrentPlay(Util.getCurrentSongPlay());
				}
			}
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		mBottomPlayer.startUpdate();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mBottomPlayer.stopUpdate();
	}

	ShowHideBottomPlayerListener showHideBottomPlayerListener = new ShowHideBottomPlayerListener() {

		@Override
		public void onShowBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.VISIBLE);
		}

		@Override
		public void onHideBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.GONE);
		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_select_song_play:
			if (mAdapter != null && mAdapter.getCount() > 0) {
//				Appinstant app = (Appinstant) getApplication();
//				app.tranportString.put(SelecSongPlay.KEY_LIST_SONG_TITLE,
//						"Bài hát trong máy");
				mAdapter.selectSongPlay("Bài hát trong máy");
			}
			break;
		case R.id.tv_play_all_song:
			if (mAdapter != null && mAdapter.getCount() > 0) {
				mAdapter.playAllSong("Local song");
			}
			break;

		default:
			break;
		}
	}

	private RetryListener retryListener = new RetryListener() {

		@Override
		public void onRetry() {
			// TODO Auto-generated method stub
			loadLocalSong();
		}
	};

	private void loadLocalSong() {
		if (mLoadLocalSong != null) {
			mLoadLocalSong.cancel(true);
		}
		mLoadLocalSong = new LoadLocalSong(LocalSongScreen.this, listener);
		mLoadLocalSong.execute();
	}

	private LoadLocalSongListener listener = new LoadLocalSongListener() {

		@Override
		public void startLoading() {
			// TODO Auto-generated method stub
			mLoadingView.startLoading();
		}

		@Override
		public void loadSuccess(List<Object> listLocalSong) {
			// TODO Auto-generated method stub
			mLoadingView.loadingFinish(true, null);
			if (listLocalSong.size() == 0) {
				showToast("Không có bài hát nào!");
				finish();
			} else {
				mAdapter = new SongListAdapter(LocalSongScreen.this,
						listLocalSong, true, LocalSongScreen.class.getName());
				mListSong.setAdapter(mAdapter);
			}
		}

		@Override
		public void loadFail(String message) {
			// TODO Auto-generated method stub
			mLoadingView.loadingFinish(false, null);
			if (Util.checkStringIsemptyOrNull(message)) {
				showToast("Không thành công");
			} else {
				showToast(message);
			}
		}
	};

}
